//
// Created by jiaoheng on 2024/11/12.
//
#include "calculate.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int calculate(){
    char expression[100];
    while(1){
        memset(expression,0,sizeof(expression));
        printf("请输入计算表达式，按回车开始计算\n");
        // windows环境下需要
        fflush(stdout);
        scanf("%s",expression);
        //判断包含的运算符，然后使用运算符分割表达式，转换数据类型并计算返回结果
        //printf("输入字符串长度:%d \n",strlen(expression));
        if(strcmp(expression,"exit")==0){
            break;
        }
        char *numStr1;
        char *numStr2;
        long num1;
        long num2;
        long result;
        if(strstr(expression,"+") != NULL){
            numStr1 = strtok(expression, "+");
            numStr2 = strtok(NULL, "+");
            num1 = atoi(numStr1);
            num2 = atoi(numStr2);
            result = num1+num2;
        }else if(strstr(expression,"-") != NULL){
            numStr1 = strtok(expression, "-");
            numStr2 = strtok(NULL, "-");
            num1 = atoi(numStr1);
            num2 = atoi(numStr2);
            result = num1-num2;
        }else if(strstr(expression,"*") != NULL){
            numStr1 = strtok(expression, "*");
            numStr2 = strtok(NULL, "*");
            num1 = atoi(numStr1);
            num2 = atoi(numStr2);
            result = num1*num2;
        }else if(strstr(expression,"/") != NULL){
            numStr1 = strtok(expression, "/");
            numStr2 = strtok(NULL, "/");
            num1 = atoi(numStr1);
            num2 = atoi(numStr2);
            result = num1/num2;
        }
        if(result!=NULL){
            char str[20];
            sprintf(str,"计算结果为:%d \n",result);
            printf(str);
        }else{
            printf("表达式有误，没有结果");
        }
    }
    return 0;
}
